package com.jbit.controller;


import com.jbit.common.RespBean;
import com.jbit.pojo.User;
import com.jbit.pojo.vo.UserVo;
import com.jbit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LiRenZhi
 * @since 2021-08-25
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "分页获取所有用户")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "currentPage", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "conditionUser", value = "筛选用户的条件")})
    @PostMapping("/")
    public RespBean getAllUsers(@RequestParam(defaultValue = "1") Integer currentPage,
                                @RequestParam(defaultValue = "5") Integer size,
                                @RequestBody(required = false) UserVo conditionUser) {
        return userService.getAllUsers(currentPage, size, conditionUser);
    }

    @ApiOperation(value = "启用或禁用用户")
    @ApiImplicitParam(name = "user", value = "要启用或禁用的用户")
    @PutMapping("/enableUser")
    public RespBean enableOrDisableUser(@RequestBody User user) {
        if (userService.updateById(user)) {
            return RespBean.success().message("更新成功！");
        }
        return RespBean.error().message("更新失败！");
    }

    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(name = "id", value = "用户id")
    @DeleteMapping("/{id}")
    public RespBean enableOrDisableUser(@PathVariable Integer id) {
        if (userService.removeById(id)) {
            return RespBean.success().message("删除成功！");
        }
        return RespBean.error().message("删除失败");
    }
}

